package com.boyuan.system.manager;

import com.boyuan.system.callback.ThermInfoCallback;
import com.boyuan.system.enums.HCPlayControlEnum;
import com.boyuan.system.hik.HCNetSDK;
import com.boyuan.system.model.vo.LoginVo;
import com.boyuan.system.websocket.BlockQueue;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.boyuan.system.hik.HCNetSDK.NET_DVR_GET_REALTIME_THERMOMETRY;
import static com.boyuan.system.utils.PreviewUtil.hcNetSDK;

/**
 * @author ply
 * @date 2023/11/20 - 15:57
 */
@Service
@Slf4j
public class HkCameraManager {

    @Async
    public Boolean playControlStartAsync(Integer userId,Integer channelId, String command){
        IntByReference m_err = new IntByReference(-1);
        boolean playHandle = false;
        HCPlayControlEnum controlEnum = HCPlayControlEnum.valueOf(command);
        //开始控制
        playHandle = hcNetSDK.NET_DVR_PTZControl_Other(userId,channelId,controlEnum.getCode(),0);
        if(!playHandle){
            String err = hcNetSDK.NET_DVR_GetErrorMsg(m_err);
            log.warn("(开始)控制失败!错误原因:" + m_err.getValue() + "," + err);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Async
    public Boolean playControlStopAsync(Integer userId,Integer channelId, String command) {
        IntByReference m_err = new IntByReference(-1);
        boolean playHandle = false;
        HCPlayControlEnum controlEnum = HCPlayControlEnum.valueOf(command);
        //停止控制
        playHandle = hcNetSDK.NET_DVR_PTZControl_Other(userId,channelId,controlEnum.getCode(),1);
        if(!playHandle){
            String err = hcNetSDK.NET_DVR_GetErrorMsg(m_err);
            log.warn("(停止)控制失败!错误原因:" + m_err.getValue() + "," + err);
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }

    @Async
    public Map<String, Float> getTemperature(int userId, LoginVo loginVo) throws InterruptedException {
        ThermInfoCallback thermInfoCallback = new ThermInfoCallback();
        HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND thermometry = new HCNetSDK.NET_DVR_REALTIME_THERMOMETRY_COND();
        thermometry.dwSize = thermometry.size();
        thermometry.byRuleID = 0;
        thermometry.dwChan = 2;
        thermometry.write();
        Pointer pUserData = new Memory(37);
        pUserData.setString(0, loginVo.getIp());
        pUserData.setInt(13,222);

        int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(userId, NET_DVR_GET_REALTIME_THERMOMETRY, thermometry.getPointer(), thermometry.size(), thermInfoCallback, pUserData);
        /*boolean getDVRConfig = hcNetSDK.NET_DVR_GetDVRConfig(userId.length() > 0 ? Integer.parseInt(userId) : loginVoList.get(0).getUserId(), NET_DVR_GET_THERMOMETRYRULE_TEMPERATURE_INFO, 2, thermometryrule.getPointer(), thermometryrule.size(), new IntByReference(64));
        System.out.println(getDVRConfig);
        if (!getDVRConfig) {
            log.error(loginVo.getIp()+"NET_DVR_GetDVRConfig failed, error code: "+ hcNetSDK.NET_DVR_GetLastError());
        }*/
        if (lHandle < 0){
            log.error(loginVo.getIp() + " NET_DVR_GET_REALTIME_THERMOMETRY failed, error code: " + hcNetSDK.NET_DVR_GetLastError());
            //break;
        }else{
            log.info(loginVo.getIp() + " NET_DVR_GET_REALTIME_THERMOMETRY is successful!");
        }
        /*while (true){
            int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(userId.length() > 0 ? Integer.parseInt(userId) : loginVoList.get(0).getUserId(), NET_DVR_GET_REALTIME_THERMOMETRY, thermometry.getPointer(), thermometry.size(), thermInfoCallback, pUserData);

            if (lHandle < 0){
                log.error(loginVo.getIp()+"NET_DVR_GET_REALTIME_THERMOMETRY failed, error code: "+ hcNetSDK.NET_DVR_GetLastError());
                break;
            }else{
                log.info(loginVo.getIp()+"NET_DVR_GET_REALTIME_THERMOMETRY is successful!");
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/
        Thread.sleep(500);
        hcNetSDK.NET_DVR_StopRemoteConfig(lHandle);
        Map<String,Float> temperatureMap = BlockQueue.findTemperatureByIp(loginVo.getIp());
        return temperatureMap;
    }
}
